import axios from 'axios'
import { Toast } from 'vant'
const service = axios.create({
  baseURL: 'http://luckin.zuiyu1818.cn:3000/',
  // http://10.7.171.80:3000
  // http://luckin.zuiyu1818.cn:3000/
  timeout: 300000
})

// 请求拦截
service.interceptors.request.use(config => {
  // 请求前的相关代码
  var token = localStorage.getItem('token')
  if (!token) {
    console.log('没有token')
  }
  config.headers = {
    Authorization: `Bearer ${token}`
  }
  Toast.loading({
    message: '加载中...',
    forbidClick: true,
    duration: 0
  })

  return config
})

// 响应拦截
service.interceptors.response.use(res => {
  // 响应处理
  Toast.clear()
  if (res.status >= 200 && res.status < 300) {
    if (res.data.meta.code === 200) {
      return res.data
    }
    return Promise.reject(res.data)
  }
  return Promise.reject(res)
}, res => {
  // console.log(res)
  Toast.fail({
    message: '请求失败！',
    duration: 1000,
    forbidClick: true
  })
})

export default service
